package com.fanhang.samples.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatementDemo {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private Connection conn;
	
	public StatementDemo() {
		try {
			conn = ConnectionProvider.getInstance().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void query() throws SQLException {
		Statement stmt = conn.createStatement();
		String sql = "select id,name,birthday,remark from student";
		logger.debug(sql);
		ResultSet rs = stmt.executeQuery(sql);
		int id;
		String name;
		Date date;
		String remark;
		String format = "id:%-5d  name:%-16s  date:%s  remark:%s";
		while (rs.next()) {
			id = rs.getInt(1);
			name = rs.getString(2);
			date = rs.getDate(3);
			remark = rs.getString(4);
			logger.info(String.format(format, id, name, date, remark));
		}
	}
	
	public static void main(String[] args) throws Exception {
		StatementDemo demo = new StatementDemo();
		demo.query();
	}

}
